import UIKit

class AlbumCell: UITableViewCell {

    var album: Album? {
        willSet {
            if let album = newValue {
                albumImageView.image = album.defaultAlbumImage
                albumTitle.text = album.name
                albumSubtitle.text = "\((album.images.count))"
            }
        }
    }
    
    lazy var albumImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }()
    
    lazy var albumTitle: UILabel = {
        let albumTitle = UILabel()
        
        return albumTitle
    }()
    
    lazy var albumSubtitle: UILabel = {
        let albumSubtitle = UILabel()
        
        return albumSubtitle
    }()
    
    lazy var separatorView: UIView = {
        let separatorView = UIView()
        separatorView.backgroundColor = UIColor ( red: 0.902, green: 0.902, blue: 0.902, alpha: 1.0 )
        
        return separatorView
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        accessoryType = .DisclosureIndicator
        selectionStyle = .None
        
        addSubview(albumImageView)
        addSubview(albumTitle)
        addSubview(albumSubtitle)
        addSubview(separatorView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        albumImageView.x = 5
        albumImageView.y = 5
        albumImageView.width = 68
        albumImageView.height = 68
        
        albumTitle.sizeToFit()
        let titleX = CGRectGetMaxX(albumImageView.frame) + 15
        let titleY = albumImageView.y  + 15
        albumTitle.x = titleX
        albumTitle.y = titleY
        
        albumSubtitle.sizeToFit()
        albumSubtitle.x = albumTitle.x
        let subtitleY = CGRectGetMaxY(albumTitle.frame)
        albumSubtitle.y = subtitleY + 10
        
        separatorView.x = 0
        separatorView.y = self.height - 1
        separatorView.width = self.width
        separatorView.height = 1
    }
}
